package org.android.bbdroid;

import org.android.bbdroid.gui.BBDroidTopicAdapterLazy;
import org.android.bbdroid.guiutils.Alerts;
import org.android.bbdroid.guiutils.HTMLUtils;
import org.android.bbdroid.webservice.PHPBBClient;
import org.android.bbdroid.webservice.data.Post;
import org.android.bbdroid.webservice.data.Topic;
import org.android.bbdroid.webservice.data.TopicInfo;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class BBDroidTopicActivity extends Activity
  implements AdapterView.OnItemClickListener
{
  private BBDroidTopicAdapterLazy adapter;
  //private TopicInfo info;
  private Topic topic;
  private void populateView()
  {
    ListView lv = (ListView)findViewById(org.android.bbdroid.R.id.lvPostList);
    this.adapter = new BBDroidTopicAdapterLazy(this, topic.getTopicInfo());
    lv.setAdapter(this.adapter);
    lv.setOnScrollListener(this.adapter);
    lv.setOnItemClickListener(this);
  }

  private void load(TopicInfo info){
	  final ProgressDialog dlg=Alerts.prog(this, "", 
				getString(org.android.bbdroid.R.string.loading_text), true);
	  
	  new AsyncTask<TopicInfo, Void, Topic>(){

		@Override
		protected Topic doInBackground(TopicInfo... params) {
			  return PHPBBClient.getInstance().getTopic(params[0]);
		}
		  
		@Override
		protected void onPostExecute(Topic result){
			dlg.dismiss();
			if(result==null){
				Alerts.w(BBDroidTopicActivity.this, 
						org.android.bbdroid.R.string.network_error_text, 
						org.android.bbdroid.R.string.cannot_connect_text);
				return;
			}
			topic=result;
			setup();
		}
	  }
	  .execute(info);
  }
  private void setup()
  {
    setTitle(HTMLUtils.fromHTML(topic.getTopicInfo().getTitle()));
    populateView();
  }
  
  @Override
  protected void onCreate(Bundle b)
  {
    super.onCreate(b);
    setContentView(org.android.bbdroid.R.layout.topics);
    TopicInfo info = ((TopicInfo)getIntent().getExtras().getSerializable("topic"));
    load(info);
  }

  @Override
  public Dialog onCreateDialog(int id, Bundle b)
  {
    return Alerts.onCreateDialog(this, id, b);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu paramMenu)
  {
    getMenuInflater().inflate(org.android.bbdroid.R.menu.topic_menu, paramMenu);
    return true;
  }

  
  public void onItemClick(AdapterView<?> paramAdapterView, View v, int position, long id)
  {
    
    final ProgressDialog dlg=Alerts.prog(this, "", 
			getString(org.android.bbdroid.R.string.loading_text), true);
    
    new AsyncTask<Object, Void, String[]>(){

		@Override
		protected String[] doInBackground(Object... params) {
			BBDroidTopicAdapterLazy adapter=(BBDroidTopicAdapterLazy)params[0];
			int position=(Integer)params[1];
			
			Post p = adapter.getPost(position);
			return new String[]{p.getSubject(), PHPBBClient.getInstance().getPostFullText(p)};
		}
    	
		@Override
		protected void onPostExecute(String[] result){
			dlg.dismiss();
			Alerts.i(BBDroidTopicActivity.this, 
					HTMLUtils.fromHTML(result[0]), HTMLUtils.fromHTML(result[1]));
		}
    }
    .execute(adapter,position);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem paramMenuItem)
  {
    switch (paramMenuItem.getItemId())
    {
    
    case org.android.bbdroid.R.id.post_reply_menu:
    	if (!topic.getTopicInfo().isAuthReply())
        {
          Alerts.w(this, org.android.bbdroid.R.string.not_authorized_text, org.android.bbdroid.R.string.not_authorized_post_text);
          return true;
        }
    	
        Intent i = new Intent(this, BBDroidSubmitPostTopicActivity.class);
        i.setAction(BBDroidActions.ADD_POST);
        Bundle b = new Bundle();
        b.putSerializable("topic", topic.getTopicInfo());
        i.putExtras(b);
        startActivity(i);
        return true;
	case org.android.bbdroid.R.id.update_topic_menu:
		finish();
		startActivity(getIntent());
    default:
      return super.onOptionsItemSelected(paramMenuItem);
    }
  }

  @Override
  protected void onRestart(){
	  super.onRestart();
	  
	  load(topic.getTopicInfo());
  }
}